package com.glq1218.category.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.glq1218.category.bo.AddCategoryBO;
import com.glq1218.category.bo.CategoryBO;
import com.glq1218.category.bo.QueryCategoryBO;
import com.glq1218.category.bo.UpdateCategoryBO;
import com.glq1218.category.entity.Category;
import com.glq1218.category.mapper.CategoryMapper;
import com.glq1218.category.service.CategoryService;
import com.glq1218.domain.*;
import com.glq1218.util.BeanCopyUtils;
import com.glq1218.util.ServiceUtils;
import com.glq1218.util.SortUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author glq1218
 * @description TODO
 * @since 2023/4/26 14:39
 */
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    /**
     * @param paginationParam 分页
     * @param sortParam       排序
     * @param intervalStr     时间间隔
     * @param queryCategoryBO 查询条件
     * @return 分页数据
     */
    @Override
    public PaginationResult<CategoryBO> queryCategoryList(PaginationParam paginationParam, SortParam sortParam, IntervalStr intervalStr, QueryCategoryBO queryCategoryBO) {
        // 处理时间间隔
        Interval interval = ServiceUtils.getInterval(intervalStr);
        // 分页对象
        Page<Category> categoryPage = ServiceUtils.getPage(paginationParam);
        // 构建查询条件对象
        String sortSql = "";
        if (StringUtils.hasText(sortParam.getSortField())) {
            sortSql = SortUtils.getSortSql(sortParam);
        }
        LambdaQueryWrapper<Category> lambdaQueryWrapper = Wrappers.lambdaQuery();
        lambdaQueryWrapper
                .like(StringUtils.hasText(queryCategoryBO.getName()), Category::getName, queryCategoryBO.getName())
                .eq(queryCategoryBO.getStatus() != null, Category::getStatus, queryCategoryBO.getStatus())
                .between(StringUtils.hasText(intervalStr.getUpdatedAtStart()) && StringUtils.hasText(intervalStr.getUpdatedAtEnd()),
                        Category::getUpdatedAt, interval.getUpdatedAtStart(), interval.getUpdatedAtEnd())
                .between(StringUtils.hasText(intervalStr.getCreatedAtStart()) && StringUtils.hasText(intervalStr.getCreatedAtEnd()), Category::getCreatedAt, interval.getCreatedAtStart(), interval.getCreatedAtEnd())
                // 默认就是使用主键id进行升序排序，可以配置其他字段，如修改日期等
                // .orderBy(!StringUtils.hasText(sortParam.getSortOrder()), true, Category::getId)
                .last(StringUtils.hasText(sortParam.getSortField()) && StringUtils.hasText(sortParam.getSortOrder()), sortSql);
        // 分页查询
        page(categoryPage, lambdaQueryWrapper);
        // 获取总数，如果总数为0，就直接返回
        long total = categoryPage.getTotal();
        if (total <= 0L) {
            return PaginationResult.<CategoryBO>builder().total(total).build();
        }
        // 将Category转为CategoryBO并设置角色信息
        List<Category> categoryList = categoryPage.getRecords();
        List<CategoryBO> categoryBOList = BeanCopyUtils.copyBeanList(categoryList, CategoryBO.class);
        return PaginationResult.<CategoryBO>builder().total(total).records(categoryBOList)
                .build();
    }

    /**
     * @param updateCategoryBO 更新分类业务对象
     * @return 更新结果
     */
    @Override
    public String updateCategory(UpdateCategoryBO updateCategoryBO) {
        if (updateCategoryBO != null) {
            if (!StringUtils.hasText(updateCategoryBO.getName())) {
                throw new RuntimeException("分类名称不能为空");
            }
        }
        Category category = BeanCopyUtils.copyBean(updateCategoryBO, Category.class);
        boolean update = updateById(category);
        if (!update) {
            throw new RuntimeException("更新失败");
        }
        return "更新成功";
    }

    /**
     * @param addCategoryBO 添加分类业务对象
     * @return 添加是否成功
     */
    @Override
    public String addCategory(AddCategoryBO addCategoryBO) {
        Category category = BeanCopyUtils.copyBean(addCategoryBO, Category.class);
        boolean saved = save(category);
        if (!saved) {
            throw new RuntimeException("添加失败");
        }
        return "添加成功";
    }
}
